#include "gameselect.h"
#include "ui_gameselect.h"
#include "widget.h"
#include "gameroom.h"
#include <QPainter>
#include <QPixmap>
#include <QPushButton>
#include <QDebug>
#include <QSound>
#include <QTextEdit>
#include <QFile>
#include <QTextStream>
#include <QFont>
Gameselect::Gameselect(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Gameselect)
{
    ui->setupUi(this);
    this->setFixedSize(800,600);

    //设置三种模式选择的按钮：简单、正常和困难
    QFont  font("华文行楷",30);
    QPushButton* simplyModeBtn = new QPushButton(this);
    QPushButton* normalModeBtn = new QPushButton(this);
    QPushButton* hardModeBtn = new QPushButton(this);
    QString btnStyle = ("QPushButton {"
                        "border: 2px solid #8f8f91;"        // 设置边框
                        "border-radius: 15px;"              // 设置圆角半径
                        "background-color: #d1d1d1;"        // 设置背景颜色
                        "padding: 5px 10px;"                // 设置内边距
                        "}"
                        "QPushButton:hover {"
                        "background-color: #c0c0c0;"        // 设置鼠标悬停时的背景颜色
                        "}"
                        "QPushButton:pressed {"
                        "background-color: #a9a9a9;"        // 设置按钮按下时的背景颜色
                        "}");
    simplyModeBtn->setFont(font);
    simplyModeBtn->setStyleSheet(btnStyle);
    simplyModeBtn->move(this->width()*0.1,this->height()*0.25);
    simplyModeBtn->setText("简单模式");

    connect(simplyModeBtn,&QPushButton::clicked,[=](){
        this->close();
        QSound::play(":res/clicked.wav");
        GameRoom *gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();

        gameRoom->setTimeout(300);
    });

    normalModeBtn->setFont(font);
    normalModeBtn->setStyleSheet(btnStyle);
    normalModeBtn->move(this->width()*0.1,this->height()*0.25+75);
    normalModeBtn->setText("正常模式");
    connect(normalModeBtn,&QPushButton::clicked,[=](){
        this->close();
        QSound::play(":res/clicked.wav");
        GameRoom *gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();
        gameRoom->setTimeout(200);
    });

    hardModeBtn->setFont(font);
    hardModeBtn->setStyleSheet(btnStyle);
    hardModeBtn->move(this->width()*0.1,this->height()*0.25+150);
    hardModeBtn->setText("困难模式");
    connect(hardModeBtn,&QPushButton::clicked,[=](){
        this->close();
        QSound::play(":res/clicked.wav");
        GameRoom *gameRoom = new GameRoom;
        gameRoom->setGeometry(this->geometry());
        gameRoom->show();

        gameRoom->setTimeout(100);
    });
//    qDebug()<<hardModeBtn->size();

    //历史记录按钮
    QPushButton *hisBtn = new QPushButton(this);
    hisBtn->setFont(font);
    hisBtn->setStyleSheet(btnStyle);
    hisBtn->move(this->width()*0.1,this->height()*0.25+225);
    hisBtn->setText("历史记录");
//    hisBtn->setFixedSize(200+this->width()*0.1,50);
//    qDebug()<<hisBtn->size();

    //23.历史记录
    QFont qfont("华文行楷",20);
    connect(hisBtn,&QPushButton::clicked,[=]()
    {
        QWidget *widget = new QWidget;
        widget->setWindowTitle("历史游戏记录");
        widget->setFixedSize(400,300);


        QTextEdit *edit = new QTextEdit(widget);
//        qfont.setPointSize(20);

        edit->setFont(qfont);
        edit->setFixedSize(400,300);

        QFile file("score.txt");
        if(file.open(QFile::ReadOnly | QFile::Text))
        {
            QTextStream in(&file);
            while (!in.atEnd())
            {
                QString line = in.readLine();
                qDebug() << line;  // Debug:输出读取到的每一行内容
                edit->append(line);
            }

        }
        widget->show();
        file.close();
    });
    //返回按钮

    QPushButton *retBtn = new QPushButton(this);
    retBtn->setIcon(QIcon(":res/back.png"));
    retBtn->move(this->width()*0.4,this->height()*0.25);
    retBtn->setFixedSize(50,50);
    retBtn->setIconSize(retBtn->size());
    retBtn->setStyleSheet("QPushButton{border:0px;background:green;}");
    connect(retBtn,&QPushButton::clicked,[=]()
    {
        this->close();
        QSound::play(":res/clicked.wav");
        Widget* gamehall = new Widget;
        gamehall->show();
    });
}

Gameselect::~Gameselect()
{
    delete ui;
}

void Gameselect::paintEvent(QPaintEvent *event)
{
    (void)event;

    QPainter painter(this);
    QPixmap pix(":res/game_select.png");

    painter.drawPixmap(0,0,this->width(),this->height(),pix);

}
